﻿var currentDrag = "";
var lastAction = new Date().getTime();

function ResetRefresh() {
    var now = new Date().getTime();
    lastAction = now;
}

$(document).ready(function() {

    $(document).bind("mousemove", ResetRefresh);
    $(document).bind("keypress", ResetRefresh);

    // Drag and Drop Task
    $(".stickynote").draggable({
        drag: function() {
            currentDrag = $(this).attr("id").substring(5);
        },
        handle: "h4"
    });
    $(".swimlane").droppable({
        drop: function() {
            $("#Task_" + currentDrag).remove().appendTo("#" + $(this).attr("id")).css({ top: "0", left: "0" }).draggable({
                drag: function() {
                    currentDrag = $(this).attr("id").substring(5);
                },
                handle: "h4"
            });

            // remove task from DOM and add it to dropped stream (if different)
            // AJAX call to update task's stream (if different)
            // If dropped elsewhere, ping it back to origin
            $.ajax({
                url: updateAddress,
                dataType: "json",
                data: {
                    "taskId": currentDrag,
                    "streamId": $(this).attr("id").substring(7)
                },
                success: function() {
                    EqualiseSwimLanes();
                },
                error: function() {
                    alert("Sorry, I couldn't save that.");
                }
            });
        }
    });

    // Inline Edit
    $(".stickynote").live("dblclick", function() {
        // Remove any other edit boxes
        $(".editzone").remove();
        // Get edit form for this task
        var taskId = $(this).attr("id").substring(5);
        $.get(inlineEditAddress + "/" + taskId, function(response) {
            response = '<div class="editzone">' + response + '</div>';
            $("#Task_" + taskId).prepend(response);
            setUpDatePickers($("#" + taskId)[0]);
            EqualiseSwimLanes();
        });
    });
    $(".editzone").live("dblclick", function() {
        return false;
    });

    // Click To Progress
    $(".stickynote .progress").live("click", function(e) {
        var taskId = $(this).parent().parent().attr("id").substring(5);
        var offset = $(this).offset();
        var x = (e.pageX - offset.left);
        var barWidth = $(this).width();

        var percentage = Math.round((x / barWidth) * 10, 0) * 10;

        $(".bar", this).attr("title", percentage).html("<span>" + percentage + "%</span>");
        AnimateProgressBars(this);

        $.ajax({
        url: progressAddress,
            dataType: "json",
            data: {
                "taskId": taskId,
                "progress": percentage
            },
            success: function() {
                // Do nothing!
            },
            error: function() {
                alert("Sorry, I couldn't save that.");
            }
        });
    });
});
